package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.KnowledgeSportDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.KnowledgeSport;
import com.itheima.service.KnowledgeSportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 知识库-运动库服务实现类
 */
@Service(interfaceClass = KnowledgeSportService.class)
@Transactional
public class KnowledgeSportServiceImpl implements KnowledgeSportService {
    @Autowired
    private KnowledgeSportDao knowledgeSportDao;

    //批量添加运动数据
    @Override
    public void addAll(List<KnowledgeSport> knowledgeSportList) {
        if (knowledgeSportList != null && knowledgeSportList.size() > 0) {
            for (KnowledgeSport knowledgeSport : knowledgeSportList) {
                //检查此运动数据（编码）是否存在
                long count = knowledgeSportDao.findCountByCode(knowledgeSport.getCode());
                //如果存在，执行修改操作
                if (count > 0) {//根据运动ID修改运动数据
                    knowledgeSportDao.editById(knowledgeSport);
                } else {
                    //不存在，执行添加操作
                    knowledgeSportDao.add(knowledgeSport);
                }
            }
        }
    }

    //新增运动库数据
    @Override
    public void add(KnowledgeSport knowledgeSport) {
        //调用DAO新增方法
        knowledgeSportDao.add(knowledgeSport);
    }

    //分页查询运动库数据
    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        //调用分页助手
        PageHelper.startPage(currentPage, pageSize);
        //调用DAO层根据条件查询方法
        Page<KnowledgeSport> page = knowledgeSportDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    //编辑运动数据
    @Override
    public void edit(KnowledgeSport knowledgeSport) {
        knowledgeSportDao.editById(knowledgeSport);
    }

    //删除运动数据
    @Override
    public void delete(Integer id) {
        knowledgeSportDao.deleteById(id);
    }
}
